<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="省/市/区县:" prop="pacc" label-width="100px">
        <area-cascader
          class="area-cascade-large"
          type="text"
          v-model="qurery_pcrs"
          :data="$pcaa"
          :level="2"
          separator="/"
        >
        </area-cascader>
      </el-form-item>
      <el-form-item label="检测地点" prop="place_name">
        <el-input
          v-model="queryParams.place_name"
          placeholder="请输入检测地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!--      <right-toolbar-->
      <!--    :showSearch.sync="showSearch"-->
      <!--    @queryTable="getList"-->
      <!--  ></right-toolbar>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="groupCheckTaskList"
      style="width: 100%;"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="序号"  type="index" width="60"></el-table-column>
      <el-table-column label="编号" align="center" prop="uuid" width="295" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="time_start"
        min-width="60px"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="time_end"
        min-width="60px"
      />
      <el-table-column
        label="持续时长"
        align="center"
        prop="duration"
        min-width="60px"
      />
      <el-table-column
        label="已预约/预约总量"
        align="center"
        prop="used_and_sum_num"
        min-width="60px"
      />
      <el-table-column
        label="预约开始时间"
        align="center"
        prop="limit_order_time"
        min-width="60px"
      />
      <el-table-column
        label="预约检测点"
        align="center"
        prop="place_all_name"
        min-width="60px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total , sizes , prev, pager, next , jumper"
      :page-size="queryParams.pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :total="total"
      :current-page="queryParams.pageNum"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :before-close="back"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="10">
            <el-form-item
              label="开始时间:"
              prop="time_start"
              label-width="100px"
            >
              <el-date-picker
                v-model="form.time_start"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期时间"
                align="right"
                @change="countDuration"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="结束时间:" prop="time_end" label-width="100px">
              <el-date-picker
                v-model="form.time_end"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期时间"
                align="right"
                @change="countDuration"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="预约总量:" prop="sum_num" label-width="100px">
              <el-input v-model="form.sum_num" placeholder="请输入预约总量" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              label="已预约:"
              prop="used_num"
              label-width="100px"
              v-if="showUsedNum"
            >
              <el-input v-model="form.used_num" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item
              label="预约时间:"
              prop="limit_order_time"
              label-width="100px"
            >
              <el-date-picker
                v-model="form.limit_order_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期时间"
                align="right"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              v-if="showDuration"
              label="持续时间:"
              prop="duration"
              label-width="100px"
            >
              <el-input
                v-model="form.duration"
                disabled
                placeholder="持续时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="检测点:" label-width="100px">
              <area-cascader
                class="area-cascade-large"
                type="text"
                v-if="showPacc"
                v-model="pcrs"
                :data="$pcaa"
                :level="2"
                separator="/"
                @change="getcheckPlace(pcrs)"
              >
              </area-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item prop="place_uuid" label-width="100px">
              <el-select
                v-model="form.place_uuid"
                clearable
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in checkPlaceOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addGroupCheckTask,
  getGroupCheckTaskList,
  getGroupCheckTask,
  delGroupCheckTask,
  updateGroupCheckTask,
} from "@/api/group_check_task/group_check_task";
import { getcheckPlaceByPcrs } from "@/api/check-place/check-place";
import { formatGap } from "@/api/dateTime/index";
export default {
  name: "check-place",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      uuids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      qurery_pcrs: [],
      // 省市区街道信息
      pcrs: [],
      // 检测点信息
      checkPlaceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: "",
        city: "",
        region: "",
        street: "",
        place_name: "",
      },
      // table数据
      groupCheckTaskList: [],
      // 表单参数
      form: {
        time_start: "",
        time_end: "",
        limit_order_time: "",
        place_uuid: "",
        duration: "",
        sum_num: "",
      },
      showPacc: false, //省市区控件，重载，通过v-if
      showDuration: true, // 显示持续时间
      showUsedNum: false,
      // 表单校验
      rules: {
        time_start: [
          { required: true, message: "起始时间不能为空", trigger: "blur" },
        ],
        time_end: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        sum_num: [
          { required: true, message: "预约总量不能为空", trigger: "blur" },
        ],
        limit_order_time: [
          { required: true, message: "可预约时间", trigger: "blur" },
        ],
        place_uuid: [
          { required: true, message: "公告类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      // 将省市区控件中的数组，转成对应的省、市、区字段
      this.queryParams = this.switchPcrs(this.qurery_pcrs, this.queryParams);
      console.log(this.queryParams)
      let params = this.$qs.stringify(this.queryParams);
      getGroupCheckTaskList(params).then((response) => {
        this.groupCheckTaskList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分页页码改变时
    handleSizeChange(size) {
      this.queryParams.pageSize = size;
      this.getList();
    },
    handleCurrentChange(current) {
      this.queryParams.pageNum = current;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.showPacc = false;
      this.reset();
    },
    // 弹出框关闭按钮触发事件
    back() {
      this.open = false;
      this.showPacc = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        time_start: "",
        time_end: "",
        limit_order_time: "",
        place_uuid: "",
        duration: "",
        sum_num: "",
        place_uuid: "",
      };
      // this.resetForm("form")
    },
    // 处理pcrs,将其转存为form.province,city,region,street
    switchPcrs(pcrs, form) {
      if (pcrs.length !== 0) {
        form.province = pcrs[0];
        form.city = pcrs[1];
        form.region = pcrs[2];
        form.street = pcrs[3];
      }
      return form;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        place_name: "",
        createBy: "",
      };
      this.qurery_pcrs = []
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.uuids = selection.map((item) => item.uuid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.showUsedNum = false; // 新增时，隐藏已预约数量
      this.showPacc = true;
      this.open = true;
      this.title = "新增检测组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid;
      getGroupCheckTask(uuid).then((response) => {
        this.form = response.data;
        this.pcrs = response.data.pcrs;
        this.showPacc = true;
        this.showUsedNum = true;
        this.open = true;
        this.title = "修改检测组";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.uuid != undefined) {
            let params = this.$qs.stringify(this.form);
            updateGroupCheckTask(params).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.showPacc = false;
              this.getList();
            });
          } else {
            this.showDuration = false; //动态更新持续时间字段
            let params = this.$qs.stringify(this.form);
            addGroupCheckTask(params).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.showPacc = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.uuids;
      this.$modal
        .confirm('是否确认删除编号为"' + uuids + '"的数据项？')
        .then(function () {
          return delGroupCheckTask(uuids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    countDuration() {
      this.showDuration = false;
      this.form.duration = formatGap(this.form.time_start, this.form.time_end);
      this.showDuration = true;
    },
    // 根据pcrs，省市区街道信息，查询核酸监测点信息
    getcheckPlace(pcrs) {
      let temp_pcrs = {}
      temp_pcrs = this.switchPcrs(pcrs,temp_pcrs)
      getcheckPlaceByPcrs(temp_pcrs).then((response) => {
        const { data } = response;
        if (data.length > 0) {
          this.checkPlaceOptions = data;
          // this.form.place_uuid = data[0].value;
        } else {
          this.checkPlaceOptions = [];
          this.form.place_uuid = "";
        }
      });
    },
  },
};
</script>
<style lang="scss">
.area-select.large {
  width: 100%;
  line-height: 20px;
}
</style>
